package com.example.nettydemo.controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import com.example.nettydemo.pojo.ro.TelCallRecordItemVO;
import com.example.nettydemo.pojo.ro.TelCallRecordRO;
import com.example.nettydemo.pojo.ro.TelCardAuthRO;
import com.example.nettydemo.pojo.vo.TelCardAuthVO;
import com.example.nettydemo.utils.BeanToFormattedString;
import io.micrometer.common.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Author: wujing
 * Date:   2025/5/14 0:10
 * Description:
 */
public class TestController {

    public static void main(String[] args) throws IllegalAccessException, ParseException, UnsupportedEncodingException {

        String c = "123456789      ABCDEFGHIJ        123456789012345678stu12345678901234502202506120601220002560206980218              202506130901320001360206980219              ";

        TelCallRecordRO parsedBean2 = new TelCallRecordRO();
        BeanToFormattedString.parseStringToBean(c, parsedBean2);
        System.out.println("解析后的 device_msisdn: " + parsedBean2);

        if (StringUtils.isNotBlank(parsedBean2.getCallItems())) {
            List<TelCallRecordItemVO> telCallRecordItemVOList = new ArrayList<>();
            if(parsedBean2.getCallItems().length() != parsedBean2.getCallTimes()*44){
                throw new RuntimeException("解析错误");
            }
            IntStream.range(0, parsedBean2.getCallTimes()).forEach(i -> {
                try {
                    int beginIndex = i * 44;
                    int endIndex = (i + 1) * 44;
                    TelCallRecordItemVO telCallRecordItemVO = new TelCallRecordItemVO();
                    BeanToFormattedString.parseStringToBean(parsedBean2.getCallItems().substring(beginIndex,endIndex), telCallRecordItemVO);
                    telCallRecordItemVOList.add(telCallRecordItemVO);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            });

            System.out.println("解析后的 device_msisdn: " + telCallRecordItemVOList);
        }

        TelCardAuthRO bean = new TelCardAuthRO();
        bean.setDeviceMsisdn("123456789");
        bean.setDeviceId("ABCDEFGHIJ");
        bean.setCardId("123456789012345678");
        bean.setStudentId("stu123456789012345");
        bean.setRequestTime(Timestamp.from(Instant.now()));


        // 将 Bean 转换为字符串
        String result = BeanToFormattedString.convertBeanToString(bean);
        System.out.println("转换后的字符串: " + result);

        // 解析字符串回 Bean
        TelCardAuthRO parsedBean = new TelCardAuthRO();
        BeanToFormattedString.parseStringToBean(result, parsedBean);

        // 输出解析后的 Bean 值
        System.out.println("解析后的 device_msisdn: " + parsedBean.getDeviceMsisdn());
        System.out.println("解析后的 device_id: " + parsedBean.getDeviceId());
        System.out.println("解析后的 card_id: " + parsedBean.getCardId());
        System.out.println("解析后的 student_id: " + parsedBean.getStudentId());
        System.out.println("解析后的 request_time: " + parsedBean.getRequestTime());


        TelCardAuthVO telCardAuthVO = new TelCardAuthVO();
        telCardAuthVO.setValidFlag(1);
        telCardAuthVO.setFamilyNum(3);
        telCardAuthVO.setFamilyMsisdn(List.of("13800138000", "13800138001", "13800138002"));
        telCardAuthVO.setFamilyRelation(List.of("",BeanToFormattedString.strChangeToGB18030("爸爸"),BeanToFormattedString.strChangeToGB18030("妈妈")));
        telCardAuthVO.setMessageNum(0);
        telCardAuthVO.setSystemTime(Timestamp.from(Instant.now()));


        ;
        System.out.println("转换后的字符串: " + BeanToFormattedString.convertBeanToString(telCardAuthVO));

    }
}
